Ext.define('PJ.view.messagemanagement.SendMessageList', {
    extend: 'AM.default.DefaultList',
    requires: ['AM.default.DefaultSearchForm', 
               'AM.default.DateTime', 
               'AM.default.DefaultDateField',
               'PJ.view.messagemanagement.ReadPeopleList'//已读人数列表
               ],
    initComponent: function(){
    	var me = this;
        me.columns = [{
    		header: '通知标题',
    		dataIndex: 'newsTitle',
    		flex: 1,
            minWidth: 120,
    		renderer: function(value, meta, record){
    			meta.style = "white-space:normal;cursor: pointer;color:blue;";
    			meta.tdAttr = "newsTitle='" + record.get("t.news_value") + "'newsId='"+record.get("newsId")+"'newsTypeCode='"+record.get("newsTypeCode")+"'";
    			return value;
    		},
    		listeners: {
    			click: function(g, clm){
    				var newsId = clm.getAttribute("newsId");
    				var newsTitle = clm.getAttribute("newsTitle");
    				var newsType = clm.getAttribute("newsTypeCode");
    				var win = Ext.create("PJ.view.messagemanagement.View", {
            			title: "查看通知",
            			text:"发送通知",
            			grid:me
            		}).show();
    				var actionName = "load";
    				if(gj_onlineLearning_constant.loadType == "jsonp"){
    					actionName = "loadJsonp";
    				}
    				win.down("form").getForm().doAction(actionName,{
    					url:me.store.preUpdateUrl,
    					params:{
    						newsId: newsId
    					},
    					success:function(f,action){
    						var data = action.response.responseText;
    	        			var json = gj_onlineLearning_constant.loadType=="ajax" ? Ext.decode(data, true) : action.response;
    	        			var form = win.down('form');	 
    	        			var baseForm = form.getForm();
    	        			if(newsType=="xxlx_pxb"){
    	        				baseForm.findField("specialName").setVisible(true);
    	        				baseForm.findField("specialName").setValue(json.data.className);
    	        				win.setHeight(360);
    	        			}else{
    	        				baseForm.findField("specialName").setVisible(false);
    	        				win.setHeight(320);
    	        			}
    	        			baseForm.findField("newsType").setValue(json.data.newsType.dataName);
    	        			baseForm.findField("newsTitle").setValue(json.data.newsTitle);
    	        			//取到值后直接传给model
    	                	var model = Ext.create("PJ.model.messagemanagement.AttachmentList");
    	                    model.set({
    	                        "attachmentId": json.data.attachmentId,
    	                        "attachName": json.data.attachName,
    	                        "attachType":  json.data.attachType,
    	                        "fileSize": json.data.fileSize,
    	                    });
    	                    var record = new Array();
    	                    var store = form.down("attachmentList").getStore();
    	                    record.push(model);
    	                    store.add(record);
    					}
    				});
    			}
    		}
    	}, {
    		header: '通知类型',
    		dataIndex: 't.newsType'
    	}, {
            header: '通知内容',
            dataIndex: 'newsValue',
    		flex: 1.5,
    		renderer: function(value, metaData, record){
    			metaData.tdAttr = 'data-qtip="' + value + '"';
    			return value;
    		}
        },{
            header: '发送时间',
            dataIndex: 't.sendTime',
            width: 120 
        }, {
            header: '已读人数/总人数',
            dataIndex: 't.readNum',
            width: 100 ,
            renderer: function(value, meta, record){
            	var receiveNumAll = record.get('receiveNumAll');
                meta.style = "cursor: pointer;color: blue;";
            	meta.tdAttr = "data-qtip='" + value + "' newsId=" + record.get("newsId");
            	if(record.get("isNewReply")==1){
            		return value + "/" + receiveNumAll+ "<span class='newSignIco' style='margin:0 10px '></span>";
            	}else{
            		return value+ "/" + receiveNumAll;
            	}
            },
            listeners: {
            	click: function(g, clm){
            		var newsId = clm.getAttribute("newsId");
            		Ext.create('AM.default.DefaultWindowView', {
                        title: "查看接收人员",
                        height:450,
                        width: 700,
                        newsId:newsId,
                        grid: me,
                        items: [{
                            xtype: 'readPeopleList'
                        }],
                        listeners: {
                        	close: function(){
                        		me.getStore().load();
                        	}
                        }
                    });
            	}
            }
        }];
        me.store = Ext.create('PJ.'+ prototype +'.messagemanagement.SendMessageList', {
            isLoad: false
        });
        this.loadType = gj_onlineLearning_constant.loadType;//加载方式
        me.callParent(arguments);
    },
  //发送
	onAddClick:function(){
        var me = this;
        win = Ext.create('PJ.view.messagemanagement.Edit',{
        	title: "发送通知",
        	width: 520,
        	height:300,
        	flagStr:"msg",
        	grid: me
        }).show();;
//        win.down('panel').getForm().findField("newsTypeCode").setValue("xxlx_tz");
    },
	//删除
	onDeleteClick:function(){
  	  var me = this;
  	  var store = me.getStore();
  	  var records = me.getSelectionModel().getSelection();
  	  for(var i=0;i<records.length;i++){
  		  if(records[i].get("t.publishState")==1){
  			Ext.Msg.show({
  				  title: '提示',
  			  	  msg  : '不允许删除已发布的数据！',
  			  	  icon :Ext.Msg.WARNING,
  			  	  buttons:Ext.Msg.OK
  			  });
  			  return;
  		  }
  	  };
  	Ext.Msg.confirm({
		  title:'提示',
		  msg:'您确定要删除这些数据吗?',
		  icon:Ext.Msg.WARNING,
		  buttons:Ext.Msg.OKCANCEL,
		  fn:function(id){
			  if(id=='ok'){
				  var newsId= new Array();
				  Ext.Array.each(records,function(name,index){
					  newsId.push(name.get('newsId'));
				  });
				  if(gj_onlineLearning_constant.loadType == "jsonp"){
					  var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
                  		loadType: gj_onlineLearning_constant.loadType,
                          url: store.deleteUrl,
                  		  extraParams: {
                  			newsIds: newsId
                          }
                  	});
                  	store1.load(function(rec, operation, success){
                  		if(success){
                              Ext.Msg.alert('通知', '删除成功！', function(){
                                  var store = me.getStore();
                                  store.load();
                              });
                          }else{
                              Ext.Msg.alert('通知', operation.error);
                          }
                  	});
				  }else{
					  Ext.Ajax.request({
						  url:store.deleteUrl,
						  params: {
							  newsIds: newsId
	                       },
						  success:function(response){
							  var data=response.responseText;
							  var json=Ext.decode(data,true);
							  if(json.success){
								  Orchid.alert('提示','删除成功!',function(){
									  store.load();
								  });
							  }else{
								  Orchid.alert('提示',json.msg);
							  }
						  }
					  });
				  }
			  }
		  }
	  });
	},
    listeners: {
    	selectionchange: function(selModel, selected){
            this.down('#' + this.oldId + "Delete").setDisabled(selected.length === 0);
        },
    	viewready: function(){
    		var me = this;
    		me.addDocked([{
    			dock: 'top',
    			xtype: "searchform",
    			columnCount: 3,
    			fieldDefaults: {
    				labelAlign: 'right',
    				labelWidth: 70
    			},
    			items: [{
    				xtype: "textfield",
    				fieldLabel: '通知内容',
    				name: "searchNewsValue",
    				maxLength: "50",
    				emptyText: "请输入通知内容"
    			},{
                    xtype: "combo",
                    fieldLabel: '通知类型',
                    name: "searchNewsCode",
                    store: Ext.create("Ext.data.ArrayStore", {
                        singleton: true,
                        fields: [{
                            name: 'value',
                            mapping: "dataCode",
                            type: 'string'
                        }, {
                            name: 'text',
                            mapping: "dataName",
                            type: 'string'
                        }],
                        proxy: {
                            type: gj_onlineLearning_constant.loadType == "jsonp" ? gj_onlineLearning_constant.loadType:'ajax',
                            url : me.store.messageTypeUrl+"?typeCode=xxlx",
                            xtype: 'combo', 
                            reader: Ext.create('Ext.data.reader.Json', {
                                type: 'json',
                                root: 'data',
                                successProperty: 'success'
                            })
                        },
                        autoLoad: true
                    }),
                    valueField: 'value',
                    displayField: 'text',
                    editable: false,
                    emptyText: "请选择"
                },{
    				xtype: "textfield",
    				fieldLabel: '接收人',
    				name: "searchReceiveUser",
    				maxLength: "50",
    				emptyText: "请输入接收人"
    			}, {
    				xtype: 'defaultDate',
    				fieldLabel: '发送时间',
    				name: 'searchBeginDate',
    				id: 'searchTrainingManagementMSG',
    				vtype: 'startLessEndDate',
    				startLessEndDate: {
    					targetCmpId: 'searchTrainingManagementMsg',
    					isStart: true
    				},
    				emptyText: "请输入开始时间"
    			}, {
    				xtype: 'defaultDate',
    				fieldLabel: '~',
    				labelSeparator: '',
    				//labelWidth: 20,
    				name: 'searchEndDate',
    				id: 'searchTrainingManagementMsg',
    				vtype: 'startLessEndDate',
    				vtypeText: '结束时间不能早于开始时间！',
    				startLessEndDate: {
    					targetCmpId: 'searchTrainingManagementMSG',
    					isStart: false
    				},
    				emptyText: "请输入结束时间"
    			}],
    			grid: this
    		}, {
                xtype: 'toolbar',
                dock: 'top',
                displayInfo: true,
                items: [{
					text: '发送通知',
					iconCls: 'addIco',
					disabled: false,
					scope: this,
					handler: this.onAddClick
				}, "-",{
					text: '删除',
					iconCls: 'deleteIco',
					itemId: this.oldId + "Delete",
					disabled: true,
					scope: this,
					handler: this.onDeleteClick
				},"->", this.countSelectPanel]
            }]);
            var store = this.store;
            var params = store.proxy.extraParams;
            var newParams = {};
            Ext.apply(params, newParams);
            store.isLoad = true;
            store.load();
    		// 触发权限过滤事件
    		this.fireEvent("permission", this);
    	}
    }
});
